package com.bulemobi.activity.ac_home_page;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.*;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bulemobi.activity.Ac_CarService.Ac_Emergency;
import com.bulemobi.activity.ac_message.Ac_Message;
import com.bulemobi.activity.ac_nearby.Ac_NearByYouSelf;
import com.bulemobi.activity.ac_user_centre.PersonCentreActivity;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.AllView;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;

/**
 * Created by 武 on 2014/9/15 0015.
 * 首页
 */
public class Ac_HomePage extends BaseActivity implements View.OnClickListener{
    private AllView allView = new AllView();
    //违章的动画
    private ScaleAnimation scaleAnimation;
    private TranslateAnimation translateAnimation;
    private RelativeLayout.LayoutParams weiZhangParams;
    private RotateAnimation rotateAnimation;
    private AnimationSet animationSet;
    private RelativeLayout.LayoutParams params;
    private ActionBarView actionBarView;
    //天气的动画
    private ScaleAnimation weatherScAnimation;
    private TranslateAnimation weatherTrAnimation;
    private AnimationSet weatherAnimationSet;
    //添加车辆动画
    private ScaleAnimation addScAnimation;
    private TranslateAnimation addTrAnimation;
    private AnimationSet addAnimatiionSet;
    private int viewCenterX;//view的x轴中心点
    private int viewCenterY;//view的Y轴中心点

    //个人中心
    @ViewInject(id = R.id.ac_tv_userCentre)
    TextView ac_tv_userCentre;
    @ViewInject(id = R.id.ac_tv_msg)
    TextView ac_tv_msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        initActionBar();
        initView();
        initClick();
    }


    /**
     * 初始化actionBarView
     */
    private void initActionBar() {
        ac_tv_msg.setOnClickListener(this);
        ac_tv_userCentre.setOnClickListener(this);
    }

    /**
     * 初始化点击事件
     */
    private void initClick() {
        //违章查询的点击事件,
        allView.lay_weiZhang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                allView.lay_weiZhang.startAnimation(animationSet);
//                allView.lay_weather.startAnimation(weatherAnimationSet);
//                allView.lay_addCar.startAnimation(addAnimatiionSet);
                openActivity(Ac_NearByYouSelf.class);

            }
        });
        //天气的点击事件
        allView.lay_weather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivity(Ac_Emergency.class);
            }
        });
        //添加车辆的点击事件
        allView.lay_addCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    /**
     * 初始化3个圆点的图案
     */
    private void initView() {
        allView.lay_weiZhang = (LinearLayout) findViewById(R.id.view1);
        allView.lay_weather = (LinearLayout) findViewById(R.id.view3);
        allView.lay_addCar = (LinearLayout) findViewById(R.id.view2);
    }

    private int getWindowWidth() {
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }

    private int getWindowHeight() {
        WindowManager wm = this.getWindowManager();
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        initAnimation();
        super.onWindowFocusChanged(hasFocus);
    }

    /**
     * 初始化动画
     */
    private void initAnimation() {
        //获取allview.view1的中心坐标
        viewCenterX = (int) ((allView.lay_weiZhang.getX() + allView.lay_weiZhang.getWidth()) / 2);
        viewCenterY = (int) ((allView.lay_weiZhang.getY() + allView.lay_weiZhang.getHeight()) / 2);
        scaleAnimation = new ScaleAnimation(1.0f, 1.7f, 1.0f, 1.7f);
//        translateAnimation = new TranslateAnimation(0,(getWindowWidth()/2)
//                -viewCenterX-allView.lay_weiZhang.getWidth(),0,
//                (getWindowHeight()/2)-viewCenterY-allView.lay_weiZhang.getHeight()*2,
//                Animation.RELATIVE_TO_PARENT,Animation.RELATIVE_TO_PARENT,Animation.RELATIVE_TO_PARENT,Animation.RELATIVE_TO_PARENT);
        translateAnimation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,getWindowWidth()/2-allView.lay_weiZhang.getX()-(int)(allView.lay_weiZhang.getWidth()*1.7/2),
                Animation.ABSOLUTE,0,Animation.ABSOLUTE,getWindowHeight()/2-(int)(allView.lay_weiZhang.getHeight()*1.7/2)-allView.lay_weiZhang.getY()-70);
        animationSet = new AnimationSet(true);    //创建动画集对象
        translateAnimation.setDuration(2000);
        scaleAnimation.setDuration(2000);
        animationSet.addAnimation(scaleAnimation);       //添加位置变化动画
        animationSet.addAnimation(translateAnimation);           //添加尺寸变化动画
        animationSet.setFillAfter(true);                 //停留在最后的位置
        animationSet.setFillEnabled(true);
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                weiZhangParams = new RelativeLayout.LayoutParams((int) (allView.lay_weiZhang.getWidth()*1.7),allView.lay_weiZhang.getHeight());
                allView.lay_weiZhang.clearAnimation();
                allView.lay_weiZhang.layout((int)allView.lay_weiZhang.getLeft(),
                        (int) allView.lay_weiZhang.getTop(),
                        (int)allView.lay_weiZhang.getLeft()+allView.lay_weiZhang.getWidth(),
                        (int)allView.lay_weiZhang.getTop()+(int)(allView.lay_weiZhang.getHeight()*1.7));

                weiZhangParams.setMargins(getWindowWidth()/2-(int)allView.lay_weiZhang.getLeft()-(int)(allView.lay_weiZhang.getWidth()*1.7/2),
                        (int)getWindowHeight()/2-(int)allView.lay_weiZhang.getTop(),0,0);

                allView.lay_weiZhang.setLayoutParams(weiZhangParams);
                allView.lay_weiZhang.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        weatherScAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
//        weatherTrAnimation = new TranslateAnimation(Animation.ABSOLUTE,allView.lay_weiZhang.getX()+allView.lay_weiZhang.getWidth()*3,0,getWindowHeight()/2);
        weatherTrAnimation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,getWindowWidth()-allView.lay_weather.getWidth(),
                Animation.ABSOLUTE,0,Animation.ABSOLUTE,(getWindowHeight())/2);
        weatherScAnimation.setDuration(2000);
        weatherTrAnimation.setDuration(2000);
        weatherAnimationSet = new AnimationSet(true);
        weatherAnimationSet.addAnimation(weatherTrAnimation);
        weatherAnimationSet.addAnimation(weatherScAnimation);
        weatherAnimationSet.setFillAfter(true);                 //停留在最后的位置
        weatherAnimationSet.setFillEnabled(true);

        addScAnimation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f);
        addTrAnimation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,-allView.lay_addCar.getX()-allView.lay_addCar.getX(),
                Animation.ABSOLUTE,0,Animation.ABSOLUTE,allView.lay_addCar.getY()-getWindowHeight()/2);
        addScAnimation.setDuration(2000);
        addTrAnimation.setDuration(2000);
        addAnimatiionSet = new AnimationSet(true);
        addAnimatiionSet.addAnimation(addTrAnimation);
        addAnimatiionSet.addAnimation(addScAnimation);
        addAnimatiionSet.setFillAfter(true);                 //停留在最后的位置
        addAnimatiionSet.setFillEnabled(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ac_tv_msg:
                //消息中心
                openActivity(Ac_Message.class);
                break;
            case R.id.ac_tv_userCentre:
                //个人中心
                openActivity(PersonCentreActivity.class);
                break;
        }
    }
}
